package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.GoodsSalesDTO;
import com.sky.dto.OrdersConfirmDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.Orders;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Mapper
public interface OrderMapper {


    /**
     * 插入订单
     * @param order
     */
    Long insert(Orders order);

    /**
     * 根据订单id查询订单
     * @param id
     * @return
     */
    @Select("select * from orders where id = #{id}")
    Orders getById(Long id);

    /**
     * 根据用户id查询订单
     */
    @Select("select * from orders where user_id = #{id}")
    Orders getByUserId(Long id);

    /**
     * 修改订单信息
     * @param orders
     */
    void update(Orders orders);

    /**
     * 根据订单号查询当前用户的该订单
     * @param orderNumber
     * @param userId
     * @return
     */
    @Select("select * from orders where number = #{orderNumber} and user_id = #{userId}")
    Orders getByNumberAndUserId(String orderNumber, Long userId);

    /**
     * 根据订单状态和下单时间查询订单
     * @param status
     * @param orderTime
     * @return
     */
    @Select("select * from orders where status = #{status} and order_time < #{orderTime}")
    List<Orders> getByStatusAndTimeLT(Integer status, LocalDateTime orderTime);

    /**
     * 分页条件查询并按下单时间排序
     * @param ordersPageQueryDTO
     */
    Page<Orders> pageQuery(OrdersPageQueryDTO ordersPageQueryDTO);

    /**
     * 统计订单状态
     * @return
     */
    @Select("SELECT\n" +
            "(SELECT COUNT(*) FROM (SELECT * FROM orders WHERE status = #{status2}) a) AS toBeConfirmed,\n" +
            "(SELECT COUNT(*) FROM (SELECT * FROM orders WHERE status = #{status3}) b) AS confirmed,\n" +
            "(SELECT COUNT(*) FROM (SELECT * FROM orders WHERE status = #{status4}) c) AS deliveryInProgress;")
    OrderStatisticsVO getStatusCount(Integer status2, Integer status3, Integer status4);

    /**
     * 接单
     */
    @Update("update orders set status = #{status} where id = #{id}")
    void confirmOrder(OrdersConfirmDTO ocd);

    /**
     * 统计指定时间区间内的菜品top10
     * @param begin
     * @param end
     * @return
     */
    List<GoodsSalesDTO> getSalesTop10(LocalDateTime begin, LocalDateTime end);


    /**
     * 根据动态条件统计营业额数据
     * @param map
     * @return
     */
    Double sumByMap(Map map);

    /**
     * 根据动态条件统计订单总数
     * @param map
     * @return
     */
    Integer countByMap(Map map);
}
